package com.bgs.controller;

import com.bgs.pojo.Classroom;
import com.bgs.pojo.vo.ClassroomVO;
import com.bgs.service.ClassService;
import com.bgs.utils.MyContent;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Log4j2
public class ClassController {

//    Logger log = LogManager.getLogger();

    @Autowired
    private ClassService cS;



    @RequestMapping("/ci")
    public String ci(){
        return "classlist";
    }


    /**
     * @Author 梁玉杰
     * @Description 全查++模糊查
     * @Date 2021/7/27 17:06
     * @Param
     * @return
     **/
    @RequestMapping("/findAllandmohu")
    @ResponseBody
    public Map<String,Object> findAllandmohu(@RequestBody ClassroomVO cVo){
        log.info("模糊查询的条件"+cVo);
        Map<String,Object> m = new HashMap<>();
        //设置分页信息(当前页码，每页显示数量)
        PageHelper.startPage(cVo.getPage(),cVo.getLimit());
        //调用分页查询账单类列表的方法
        List<Classroom> CS = cS.findAllandmohu(cVo);
        //创建分页对象
        PageInfo<Classroom> pageInfo = new PageInfo<Classroom>(CS);
        // JSONObject jsonObj = JSONObject.parseObject(sJson);
        // m = jsonObj; json对象转成map
        log.info("List<Classroom>:::"+pageInfo);
        m.put(MyContent.CODE,0);
        m.put(MyContent.MSG,"查询成功！");
        m.put(MyContent.COUNT,pageInfo.getTotal());
        m.put(MyContent.DATA,pageInfo.getList());

        return m;
    }

    /**
     * @Author 梁玉杰
     * @Description class的添加
     * @Date 2021/7/27 17:07
     * @Param
     * @return
     **/
    @RequestMapping("/addClass")
    @ResponseBody
    public Map<String,Object> addClass (@RequestBody Classroom c){
        HashMap<String, Object>  m = new HashMap<>();
        log.info("添加的"+c);
        if (cS.insertSelective(c) > 0){

            m.put("success",true);
            m.put("message","添加成功");
        }else{
            m.put("success",false);
            m.put("message","添加失败");
        }
        return m;
    }



    //修改
    @RequestMapping("/updClass")
    @ResponseBody
    public Map<String,Object> updClass(@RequestBody ClassroomVO cVO){
        Map<String,Object> m = new HashMap<>();
        log.info("修改的VO"+cVO);
        if(cS.updateByPrimaryKeySelective(cVO) > 0){
            m.put("success",true);
            m.put("message","修改成功");
        }else{
            m.put("success",false);
            m.put("message","修改 失败");
        }
        return m;
    }


    /**
     * 删除学生
     */
    @RequestMapping("/deleteBypkId")
    @ResponseBody
    public Map<String, Object> deleteBypkId(@RequestBody Integer pkId){
        Map<String,Object> m = new HashMap<>();
        if(cS.deleteById(pkId)>0){
            m.put("success",true);
            m.put("message","删除成功");
        }else{
            m.put("success",false);
            m.put("message","删除失败");
        }
        return m;
    }

    /**
     * 批量删除账单
     */
    @RequestMapping("/batchDeleteClass")
    @ResponseBody
    public Map<String, Object> batchDeleteClass(@RequestBody String pkId){
        Map<String,Object> map = new HashMap<>();
        int count = 0;
        //将字符串拆分成数组
        String[] idsStr = pkId.split(",");
        for (int i = 0; i < idsStr.length; i++) {
            count = cS.deleteById(Integer.valueOf(idsStr[i]));
            if(count>0){
                map.put("success",true);
                map.put("message","批量删除成功");
            }
        }
        //判断受影响行数是否为0
        if(count<=0){
            map.put("success",false);
            map.put("message","批量删除失败");
        }
        return map;
    }


    @RequestMapping("/threexlk")
    @ResponseBody
    public List<Classroom> threexlk(){
        List<Classroom> xlk = cS.findAllxlk();
        log.info("下拉框的1"+xlk);
        return xlk;
    }

    @RequestMapping("/threexlkbn")
    @ResponseBody
    public List<Classroom> threexlkbn(@RequestBody ClassroomVO classroom){
        log.info("下拉框的2"+classroom);
        List<Classroom> xlk = cS.findAllxlkbn(classroom);
        return xlk;
    }

    @RequestMapping("/threexlkbf")
    @ResponseBody
    public List<Classroom> threexlkbf(@RequestBody ClassroomVO classroom){
        log.info("下拉框的3"+classroom);
        List<Classroom> xlk = cS.findAllxlkbf(classroom);
        return xlk;
    }

}
